package P020;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Stack;

/**
 * @Author DJ同学
 * @Date 2021/2/9 0:07
 * @Version 1.0
 * @Name 有效的括号
 * @Problem https://leetcode-cn.com/problems/valid-parentheses/
 * 思路 堆栈（java中有现成的）
 */
public class Solution {
    @Test
    public void test01(){
        System.out.println(isValid("()[]}"));
    }
    public boolean isValid(String s) {
        boolean f=true;
        Stack<Character> stack = new Stack<>();
        char[] chars = s.toCharArray();
        for(int i = 0;i<chars.length;i++){
            if(stack.empty()){
                stack.push(chars[i]);
            }else{
                Character pop = stack.peek();
                if(chars[i]=='('||chars[i]=='{'||chars[i]=='['){
                    stack.push(chars[i]);
                }else if(chars[i]==')'&&pop=='('){
                    stack.pop();
                }else if(chars[i]=='}'&&pop=='{'){
                    stack.pop();
                }else if(chars[i]==']'&&pop=='['){
                    stack.pop();
                }else{
                    break;
                }
            }
        }
        return stack.empty()?true:false;
    }
}
